package com.njwangbo.day08.demo03.service.impl;

import com.njwangbo.day08.demo03.dao.StudentDao;
import com.njwangbo.day08.demo03.dao.impl.StudentDaoImpl;
import com.njwangbo.day08.demo03.entity.Student;
import com.njwangbo.day08.demo03.service.StudentService;

public class StudentServiceImpl implements StudentService {

    @Override
    //添加学生
    public void addStudent(Student student) {
        //学生是否已经存在
        StudentDao studentDao = new StudentDaoImpl();
        Student s = studentDao.findStudentByNo(student.getNo());
        //如果学生已经存在，则提示用户
        if (s != null) {
            return;
        }
        //如果学生不存在，则添加学生
        studentDao.insertStudent(student);
        System.out.println("学生添加成功！");
    }

    @Override
    //修改学生信息
    public void modifyStudent(int no, String name, double score) {
        //查找学生是否存在
        StudentDao studentDao = new StudentDaoImpl();
        Student student = studentDao.findStudentByNo(no);
        //如果学生不存在
        if (student == null) {
            return;
        }
        //如果学生存在，则修改学生信息
        studentDao.modifyStudentByNo(no, name, score);
        System.out.println("学生信息修改成功！");
    }

    @Override
    //删除学生
    public void removeStudent(int no) {
        //查找学生是否存在
        StudentDao studentDao = new StudentDaoImpl();
        Student student = studentDao.findStudentByNo(no);
        //如果学生不存在，则提示用户
        if (student == null) {
            System.out.println("学生不存在，无法删除！");
            return;
        }
        //如果学生存在，则删除学生
        studentDao.removeStudentByNo(no);
        System.out.println("学生删除成功！");
    }

    //查找所有学生
    @Override
    public Student[] findAllStudents() {
        StudentDao studentDao = new StudentDaoImpl();
        Student[] students = studentDao.getAllStudents();
        //如果没有学生信息，则提示用户
        if (students == null || students.length == 0) {
            return new Student[0]; // 返回空数组
        }
        return studentDao.getAllStudents();
    }

    //通过学号查找学生
    @Override
    public Student findStudentByNo(int no) {
        //查找学生是否存在
        StudentDao studentDao = new StudentDaoImpl();
        Student student = studentDao.findStudentByNo(no);
        //如果学生不存在，则提示用户
        if (student == null) {
            return null;
        }
        //如果学生存在，则打印学生信息
        return student;
    }

    //通过姓名查找学生
    @Override
    public Student findStudentByName(String name) {
        //查找学生是否存在
        StudentDao studentDao = new StudentDaoImpl();
        Student student = studentDao.findStudentByName(name);
        //如果学生不存在，则提示用户
        if (student == null) {
             return null;
        }
        //如果学生存在，则打印学生信息
        return student;
    }

    //判断学生是否存在，并根据分数从大到小排序
    @Override
    public Student[] sortStudents() {
        StudentDao studentDao = new StudentDaoImpl();
        Student[] students = studentDao.getAllStudents();
        if(students == null || students.length == 0) {
            return new Student[0]; // 返回空数组
        }
        return studentDao.sortStudents();
    }
}
